<?php

Solar::loadClass('Solar_Struct');

class Domain51_Service_Amazon_S3_Value_Object extends Solar_Struct
{
    protected $_Domain51_Service_Amazon_S3_Value_Object = array(
        'data' => array(
            'headers' => array(
                'Content-Type' => 'binary/octet-stream'
            )
        )
    );
    
    public function __get($key) {
        $value = parent::__get($key);
        if ($key == 'fullName' && empty($value)) {
            $value = $this->bucket . $this->name;
            $this->fullName = $value;
        }
        
        return $value;
    }
    
    public function __set($key, $value) {
        switch($key) {
        case 'bucket' :
            // add trailing slash
            if (!preg_match('/\/$/', $value)) {
                $value .= '/';
            }
            $this->_data['bucket'] = $value;
            break;
            
        default:
            parent::__set($key, $value);
        }
    }
    
    public function setFile($pathToFile) 
    {
        if (!file_exists($pathToFile)) {
            throw $this->_exception(
                'ERR_FILE_NOT_FOUND',
                array('pathToFile' => $pathToFile)
            );
        }
        
        if (!is_readable($pathToFile)) {
            $this->_exception(
                'ERR_FILE_NOT_READABLE',
                array('pathToFile' => $pathToFile)
            );
        }
        
        if (empty($this->headers)) {
            $this->headers = array();
        }
        $this->headers['Content-Type'] = mime_content_type($pathToFile);
        $this->body = file_get_contents($pathToFile);
        if (empty($this['name'])) {
            $this->name = basename($pathToFile);
        }
    }
}

?>